package com.itheima.sc.impl.pms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sc.api.pms.SkuStockApi;
import com.itheima.sc.mapper.pms.SkuStockMapper;
import com.itheima.sc.pojo.pms.SkuStock;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SkuStockImpl extends ServiceImpl<SkuStockMapper, SkuStock> implements SkuStockApi {

    @Override   //批量更新sku库存信息  todo:pid未使用
    public void updateByPid(Integer pid, List<SkuStock> skuStockList) {
        //根据pid查找sku集合
        LambdaQueryWrapper<SkuStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuStock::getProductId, Convert.toLong(pid));
        List<SkuStock> stockList = list(wrapper);

        //遍历集合,更新sku对象
        updateBatchById(skuStockList);
    }

    @Override   //根据商品ID及sku编码模糊搜索sku库存
    public List<SkuStock> getByPid_skuCode(Integer pid, String keyword) {
        LambdaQueryWrapper<SkuStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuStock::getProductId,Convert.toLong(pid));
        wrapper.like(StrUtil.isNotBlank(keyword),SkuStock::getSkuCode,keyword);

        List<SkuStock> skuStockList = list(wrapper);
        return skuStockList;
    }

    @Override
    public List<SkuStock> getByPid(Integer id) {
        LambdaQueryWrapper<SkuStock> wrapper = new LambdaQueryWrapper<SkuStock>().eq(SkuStock::getProductId, Convert.toLong(id));
        return list(wrapper);
    }

    @Override
    public void removeByPid(Integer pid) {
        LambdaQueryWrapper<SkuStock> wrapper = new LambdaQueryWrapper<SkuStock>().eq(SkuStock::getProductId, Convert.toLong(pid));
        remove(wrapper);
    }


}
